package  
{
	import org.flixel.FlxG;
	import org.flixel.FlxGroup;
	import org.flixel.FlxSprite;
	
	/**
	 * ...
	 * @author maliu
	 */
	public class Player extends FlxSprite 
	{
		[Embed(source = "../res/spaceman.png")]
		protected var p_playerImg:Class;
		
		protected static const PLAYER_RUN_SPEED:int = 80;
		protected static const GRAVITY_ACCELERATION:Number = 420;
		protected static const JUMP_ACCELERATION:Number = 200;
		
		public var bulletList:FlxGroup;
		private var m_currentBullet:int = 0;
		
		public function Player(_startX:Number = 100, _startY:Number = 50) 
		{
			super(_startX, _startY, null);
			this.loadGraphic(p_playerImg, true, true, 8, 8);
			this.drag.x = PLAYER_RUN_SPEED * 8;
			this.acceleration.y = GRAVITY_ACCELERATION;
			this.maxVelocity.x = PLAYER_RUN_SPEED;
			this.maxVelocity.y = JUMP_ACCELERATION;
			
			this.addAnimation("idle", [0]);
			this.addAnimation("run", [1, 2, 3, 0], 12);
			this.addAnimation("jump", [4]);
			this.addAnimation("idle_up", [5]);
			this.addAnimation("run_up", [6, 7, 8, 5], 12);
			this.addAnimation("jump_up", [9]);
			this.addAnimation("jump_down", [10]);
		}
		override public function update():void
		{
			this.acceleration.x = 0;
			if (FlxG.keys.A)
			{
				this.facing = LEFT;
				this.acceleration.x = -this.drag.x;
			}
			else if (FlxG.keys.D)
			{
				this.facing = RIGHT;
				this.acceleration.x = this.drag.x;
			}
			
			if (FlxG.keys.justPressed("SPACE") && this.onFloor)
			{
				this.velocity.y = -JUMP_ACCELERATION;
			}
			
			if (this.velocity.y != 0)
			{
				this.play("jump");
			}
			else if (this.velocity.x == 0)
			{
				this.play("idle");
			}
			else
			{
				this.play("run");
			}
			
			if (this.bulletList != null)
			{
				if (FlxG.keys.justPressed("J"))
				{
					if (FlxG.keys.W)
					{
						this.bulletList.members[m_currentBullet].shoot(x, y-4, 0, -250);
					}
					else if (FlxG.keys.S)
					{
						this.bulletList.members[m_currentBullet].shoot(x, y+4, 0, 250);
					}
					else if (this.facing == LEFT)
					{
						this.bulletList.members[m_currentBullet].shoot(x-4, y, -250, 0);
					}
					else if (this.facing == RIGHT)
					{
						this.bulletList.members[m_currentBullet].shoot(x+4, y, 250, 0);
					}
					m_currentBullet++;
					//trace("bulletList.length = "+ this.bulletList.members.length);
					//trace("m_currentBullet = "+ m_currentBullet);
					m_currentBullet %= this.bulletList.members.length;
				}
			}
			super.update();
		}
		
	}

}